package org.jeecg.modules.business.third.strategy.sap;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTaskMaterial;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskMaterialService;
import org.jeecg.modules.business.third.entity.WmsOutReceiptMaterialVo;
import org.jeecg.modules.dock.entity.dto.request.IngredientsGetRequestDTO;
import org.jeecg.modules.dock.entity.dto.request.ProductReceptionRequestDTO;
import org.jeecg.modules.dock.entity.dto.response.IngredientsGetResponseDTO;
import org.jeecg.modules.dock.entity.dto.response.ProductReceptionResponseDTO;
import org.jeecg.modules.dock.service.SapDockingRepeaterService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/*
 * 项目名称:    wms-server-jeecg
 * 接口描述:    调拨出库(辅材)
 * 创建人员:    hansen
 * 创建时间:    2023-12-6
 */
@Service(value = "c51SapOutbound")
@Slf4j
@RequiredArgsConstructor
public class C51SapOutbound implements SapOperateStrategy {

    private final IWmsOutReceiptTaskMaterialService wmsOutReceiptTaskMaterialService;
    private final SapDockingRepeaterService sapDockingRepeaterService;

    /**
     * 根据传入的map参数执行辅料领用出库操作，返回辅料领用出库操作结果列表。
     *
     * @param map 参数map，包含辅料领用出库相关信息
     * @return 辅料领用出库操作结果列表
     */
    @Override
    public List operate(Map<String, Object> map) {
        log.info("辅材领用出库(辅材)");
        List<IngredientsGetRequestDTO> list = new ArrayList<>();
        List<WmsOutReceiptMaterialVo> wmsOutReceiptMaterialVos = (List<WmsOutReceiptMaterialVo>) map.get("records");
        wmsOutReceiptMaterialVos.forEach(t -> {
            // 创建请求DTO对象
            IngredientsGetRequestDTO requestDTO = new IngredientsGetRequestDTO();
            // 验证WmsOutReceiptMaterial参数
            SapParamUtil.validWmsOutReceiptMaterial(t);
            // 根据原始系统号和原始系统行号获取WmsOutReceiptTaskMaterial记录
            WmsOutReceiptTaskMaterial one = wmsOutReceiptTaskMaterialService.getOne(new LambdaQueryWrapper<WmsOutReceiptTaskMaterial>().eq(WmsOutReceiptTaskMaterial::getOriginalSystemNumber, t.getOriginalSystemNumber())
                    .eq(WmsOutReceiptTaskMaterial::getOriginalSystemLineNumber, t.getOriginalSystemLineNumber()));
            // 验证输出任务记录
            SapParamUtil.validOutTaskRecord(one);
            // 将WmsOutReceiptTaskMaterial的额外参数解析为IngredientsGetRequestDTO对象
            IngredientsGetRequestDTO ingredientsGetRequestDTO = JSONObject.parseObject(one.getExtraText(), IngredientsGetRequestDTO.class);
            // 设置请求DTO对象的属性值
            requestDTO.setUuid(ingredientsGetRequestDTO.getUuid());
            requestDTO.setUnit(ingredientsGetRequestDTO.getUnit());
            requestDTO.setMateriel(ingredientsGetRequestDTO.getMateriel());
            requestDTO.setFactory(ingredientsGetRequestDTO.getFactory());
            requestDTO.setStorageLocation(ingredientsGetRequestDTO.getStorageLocation());
            requestDTO.setMovementType(ingredientsGetRequestDTO.getMovementType());
            requestDTO.setNumber(ingredientsGetRequestDTO.getNumber());
            requestDTO.setCostCenter(ingredientsGetRequestDTO.getCostCenter());
            requestDTO.setOrder(ingredientsGetRequestDTO.getOrder());
            // 将请求DTO对象添加到列表中
            list.add(requestDTO);
        });
        // 调用sapIngredientsGet方法获取辅料领用出库操作结果列表
        List<IngredientsGetResponseDTO> ingredientsGetResponseDTOS = sapDockingRepeaterService.sapIngredientsGet(list);
        // 返回辅料领用出库操作结果列表
        return ingredientsGetResponseDTOS;
    }

}
